package com.geeklei.media.ui.widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.util.TypedValue;

import com.geeklei.media.ui.R;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class BatteryIcon {
    private static final int BATTERY_UNIT_HEIGHT_HDPI = 38;
    private static final int BATTERY_UNIT_HEIGHT_XHDPI = 50;
    private static final int BATTERY_UNIT_HEIGHT_XXHDPI = 60;
    private static final int BATTERY_UNIT_HEIGHT_XXXHDPI = 72;
    private static BatteryIcon sBatteryIcon;
    private final int BATTERY_RANGE_LOAD = 10;
    private int mBatteryColumns;
    private int mChargeDarkLevel = -1;
    private int mChargeLevel = -1;
    private Context mContext;
    private int mDarkLevel = -1;
    private LevelListDrawable mGraphicChargeIcon;
    private LevelListDrawable mGraphicChargeIconDarkMode;
    private LevelListDrawable mGraphicIcon;
    private LevelListDrawable mGraphicIconDarkMode;
    private int mLevel = -1;

    public static BatteryIcon getInstance(Context context) {
        if (sBatteryIcon == null) {
            sBatteryIcon = new BatteryIcon(context);
        }
        return sBatteryIcon;
    }

    private BatteryIcon(Context context) {
        this.mContext = context;
        this.mBatteryColumns = 1;
    }

    public LevelListDrawable getGraphicIcon(int i) {
        if (this.mLevel == -1 || this.mLevel - i > 10 || this.mLevel - i < 0) {
            this.mGraphicIcon = generateIcon(R.raw.stat_sys_battery, i, false);
            this.mLevel = i;
        }
        return this.mGraphicIcon;
    }

    public LevelListDrawable getGraphicIconDarkMode(int i) {
        if (this.mDarkLevel == -1 || this.mDarkLevel - i > 10 || this.mDarkLevel - i < 0) {
            this.mGraphicIconDarkMode = generateIcon(R.raw.stat_sys_battery_darkmode, i, false);
            this.mDarkLevel = i;
        }
        return this.mGraphicIconDarkMode;
    }

    public LevelListDrawable getGraphicChargeIcon(int i) {
        if (this.mChargeLevel == -1 || i - this.mChargeLevel > 10 || i - this.mChargeLevel < 0) {
            this.mGraphicChargeIcon = generateIcon(R.raw.stat_sys_battery_charge, i, true);
            this.mChargeLevel = i;
        }
        return this.mGraphicChargeIcon;
    }

    public LevelListDrawable getGraphicChargeIconDarkMode(int i) {
        if (this.mChargeDarkLevel == -1 || i - this.mChargeDarkLevel > 10 || i - this.mChargeDarkLevel < 0) {
            this.mGraphicChargeIconDarkMode = generateIcon(R.raw.stat_sys_battery_charge_darkmode, i, true);
            this.mChargeDarkLevel = i;
        }
        return this.mGraphicChargeIconDarkMode;
    }

    private LevelListDrawable generateIcon(int id, int index, boolean chargeing) {
        int i3 = 0;
        LevelListDrawable levelListDrawable = new LevelListDrawable();
        ArrayList extractDrawable = extractDrawable(id);
        int size = extractDrawable.size();
        if (size > 0) {
            float f = 0.4f;
            float eachSize = 100.0f / ((float) size);
            int i4 = chargeing ? index : index + -BATTERY_RANGE_LOAD < 0 ? 0 : index - BATTERY_RANGE_LOAD;
            if (chargeing) {
                index = index + BATTERY_RANGE_LOAD > 100 ? 100 : index + BATTERY_RANGE_LOAD;
            }
            while (i3 < size) {
                int i5 = (int) f;
                int i6 = (int) (f + eachSize);
                if (i6 < i4 || i5 > index) {// modifyed by leixun
                    levelListDrawable.addLevel(i5, i6, null);
                } else {
                    levelListDrawable.addLevel(i5, i6, (Drawable) extractDrawable.get(i3));
                }
                f += eachSize;
                i3++;
            }
        }
        return levelListDrawable;
    }

    private ArrayList<Drawable> extractDrawable(int i) {
        ArrayList<Drawable> arrayList = new ArrayList();
        Resources resources = this.mContext.getResources();
        TypedValue typedValue = new TypedValue();
        InputStream openRawResource = resources.openRawResource(i, typedValue);
        Bitmap decodeStream = BitmapFactory.decodeStream(openRawResource);
        try {
            openRawResource.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (decodeStream == null) {
            return arrayList;
        }
        int i2;
        int max = Math.max(typedValue.density, 240);
        if (max == 240) {
            i2 = BATTERY_UNIT_HEIGHT_HDPI;
        } else if (max == 320) {
            i2 = BATTERY_UNIT_HEIGHT_XHDPI;
        } else if (max == 640) {
            i2 = BATTERY_UNIT_HEIGHT_XXXHDPI;
        } else {
            i2 = BATTERY_UNIT_HEIGHT_XXHDPI;
        }
        int width = decodeStream.getWidth() / this.mBatteryColumns;
        int height = decodeStream.getHeight() / i2;
        int width2 = decodeStream.getWidth() / width;
        int[] iArr = new int[(i2 * width)];
        for (int i3 = 0; i3 < height; i3++) {
            for (int i4 = 0; i4 < width2; i4++) {
                decodeStream.getPixels(iArr, 0, width, i4 * width, i3 * i2, width, i2);
                Bitmap createBitmap = Bitmap.createBitmap(iArr, 0, width, width, i2, Config.ARGB_8888);
                createBitmap.setDensity(max);
                arrayList.add(new BitmapDrawable(resources, createBitmap));
            }
        }
        decodeStream.recycle();
        return arrayList;
    }

    public void clear() {
        this.mGraphicIcon = null;
        this.mGraphicIconDarkMode = null;
        this.mGraphicChargeIcon = null;
        this.mGraphicChargeIconDarkMode = null;
        this.mLevel = -1;
        this.mDarkLevel = -1;
        this.mChargeLevel = -1;
        this.mChargeDarkLevel = -1;
    }
}
